//
//  SingerViewController.m
//  CloudKaraoke
//
//  Created by BHDMinh on 3/29/14.
//  Copyright (c) 2014 soncamedia. All rights reserved.
//

#import "SingerViewController.h"
#import "SingerDetailsViewController.h"
#import "PersonDisplayCell.h"
#import "DBInterface.h"
#import "Singer.h"

@interface SingerViewController ()
{
    
}
@end

@implementation SingerViewController

#pragma mark - Table view data source
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
	return 22;
}

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    NSInteger songcount = [[self DataForTableView:tableView] count];
    CGRect frame = [self.view bounds];
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(8.5, 0, frame.size.width, 21.0)];
    [view setAutoresizingMask:UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin];
    [view setBackgroundColor:KOK_COLOR_WHITE];
    
    UILabel *labelTitle = [[UILabel alloc] initWithFrame:view.frame];
    [labelTitle setText:[NSString stringWithFormat:@"%d ca sỹ", songcount]];
    [labelTitle setTextColor:KOK_COLOR_LIGHT_BROWN];
    
    [view addSubview:labelTitle];
    
    return view;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[self DataForTableView:tableView] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"PersonDisplayCell";
    PersonDisplayCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"PersonDisplayCell" owner:self options:nil] objectAtIndex:0];
    }
    
    Singer *person = [[self DataForTableView:tableView] objectAtIndex:indexPath.row];
    [cell configureDisplayCellForPerson:person searchString:searchString];
    
    cell.backgroundColor = [UIColor clearColor];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {
        return DISPLAY_CELL_DEFAULT_IPAD;
    }
    return DISPLAY_CELL_DEFAULT_IPHONE;
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Navigation logic may go here. Create and push another view controller.
    Singer *singer = [[self DataForTableView:tableView] objectAtIndex:indexPath.row];
    
    SingerDetailsViewController *singerDetailsController = [[SingerDetailsViewController alloc] initWithNibName:@"SingerDetailsViewController" bundle:nil];
    
    [singerDetailsController setParentItem:singer];
    
    [self.navigationController pushViewController:singerDetailsController animated:YES];
    
    [tableViewData deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - UIViewHandler
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    appDelegate = [[UIApplication sharedApplication] delegate];
    
    [self initSearchHandleBlock];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableViewData setHidden:YES];
    [self.loadingIndicator startAnimating];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    if(allData) {
        [self.tableViewData reloadData];
        [self.loadingIndicator stopAnimating];
        [self.tableViewData setHidden:NO];
    }
}

- (void) dealloc
{
    _compareBlock = nil;
}

#pragma mark - reloadData
- (void) reloadDisplayData
{
    if(allData == nil)
    {
        allData = [NSArray arrayWithArray:[[DBInterface instance] querySinger:@""]];
        [tableViewData reloadData];
        [self.loadingIndicator stopAnimating];
        [tableViewData setHidden:NO];
        if ([allData count] > 0) {
            [self.tableViewData scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
        }
    }
}

- (void) initSearchHandleBlock
{
    _compareBlock = ^SCCompareResult(Singer *obj, NSString *searchText) {
        if([obj.shortname rangeOfString:searchText options:NSCaseInsensitiveSearch].location == 0)
        {
            return SCComparePinyin;
        }
        else if([obj.titleRaw rangeOfString:searchText options:NSCaseInsensitiveSearch].location != NSNotFound)
        {
            return SCCompareTitleRaw;
        }else if ([obj.name rangeOfString:searchText options:NSCaseInsensitiveSearch].location != NSNotFound) {
            return SCCompareName;
        }
        return SCCompareNotEqual;
    };
}
@end
